package net.borderwars.terrain;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

/**
 * @author ehubbard
 *         Date: Aug 22, 2005
 *         Time: 12:50:11 PM
 */
public class Generator {
    // assumes the world is big
    static int sx = Integer.MIN_VALUE;
    static int sy = Integer.MAX_VALUE;
    static int ex = Integer.MAX_VALUE;
    static int ey = Integer.MIN_VALUE;
    Random r = new Random (10);

    short first[][] = new short[256][256];

    public static void main (String[] args) {
        Random r = new Random (1);
        int data[][] = new int[256][256];
        JFrame f = new JFrame ("Terrain");
        for (int x = 0; x < 256; x++) {
            for (int y = 0; y < 256; y++) {
                data[x][y] = Math.abs (r.nextInt ());
            }
        }
        f.setSize (400, 400);
        JPanel p = new JPanel ();
        f.setContentPane (p);
        f.setVisible (true);
        setupColors ();
        drawArray (p, data);

    }

    static private void setupColors () {
        for (int x = 0; x < 256; x++) {
            colorTable[x] = new Color (x, x, x);
        }
    }

    static Color colorTable[] = new Color[256];

    private static void drawArray (JPanel p, int data[][]) {
        for (int x = 0; x < 256; x++) {
            for (int y = 0; y < 256; y++) {
                Graphics g = p.getGraphics ();
                g.setColor (colorTable[(data[x][y] / 256 * 256 * 256)]);
                g.drawRect (x, y, 1, 1);
            }
        }
        p.repaint ();
    }

    public Generator () {
        for (int x = sx; x < ex; x++) {
            for (int y = sy; y < ey; y++) {
                first[x][y] = (short) r.nextInt ();
            }
        }

    }

    short second[][] = new short[256][256];
    short third[][] = new short[256][256];
    short fourth[][] = new short[256][256];

    short mmeters[][] = new short[1024][1024];
    short meters[][] = new short[1024][1024];
    short kmeters[][] = new short[1024][1024];
    short kkmeters[][] = new short[1024][1024];

    public short getElevation (int x, int y) {
        populateArray (first, second, x & 0xff000000 >> 6, y & 0xff000000 >> 6);
        populateArray (second, third, x & 0x00ff0000 >> 4, y & 0x00ff0000 >> 4);
        populateArray (third, fourth, x & 0x0000ff00, y & 0x0000ff00);
        return (fourth[x & 0x000000ff][y & 0x000000ff]);
    }

    private void populateArray (short[][] source, short[][] target, int sx, int sy) {
        short startPoint = source[sx][sy];
        for (int x = 0; x < 256; x++) {
            for (int y = 0; y < 256; y++) {
                target[x][y] = startPoint;
            }
        }

    }

    // Map area is 32bit by 32bit by 16bit
    // Granularity is broken up into 4 LOD
    //     1st. Top most is plates/mountains elevations 8bits  stored & static
    //
    //
    //          1 of these
    //     2nd.  16,000 km^2
    //
    //          256 of these
    //     3rd.  64 km^2
    //
    //          16K of these
    //     4th. Generated on the fly 256meter by 256meter area fractal using
    //          a random number seed of the values from, 1st, 2nd, 3rd
    //          results thrown away to make room for others
    //          24million possible of these

    private void doSquare (int width, int height) {

        int elevation[][] = new int[width][height];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                elevation[x][y] = 0;
            }
        }

        // while the length of the square size >0
        // perform diamond
        // step for each square present
        // perform square
        // step through each diamond
        // reduce number range

    }

}
